<?php

HBase::using('DAL.HPages');
HBase::using('DAL.HPagesModules');
HBase::using('DAL.HModules');
HBase::using('DAL.HModulesInstances');

class HLayout {
	public function loadModulesToPanes($alias, $panename) {			
		// Get Page ID
		$page_id = HPages::getPageIdByAlias($alias);	
		// Get page modules
		$modules = HPages::getModulesByPane($page_id, $panename);			
		
		// Render
		if (count($modules) != 0) {	
			for ($i = 0; $i < count($modules); $i++) {				
				$name = HModules::getModuleNameById($modules[$i]['module_instance_id']);					
				// Instance Id				
				$instance_id = $modules[$i]['module_instance_id'];
				// Module ID 				
				$module_id = HModulesInstances::getModuleIdByInstanceId($instance_id);				
							
				// Path to module
				$path = MODULE_PATH.SEPARATOR.$name.SEPARATOR;
								
				// Module view file - dispatcher				
				$control_key = HModulesInstances::getControlKeyById($instance_id);					
								
				// Get the view file 
				$view_file = HModulesInstances::getViewFileByModuleIdAndControlKey($module_id, $control_key);				
								
				// Get the components of module view paths
				$components = explode("_", $view_file);
				
				// Get template file name
				$tmp = explode(".",$components[count($components) - 1]);				
				
				// Require_once
				$path = SYSTEM_PATH.SEPARATOR.'modules'.SEPARATOR;				
				for ($i = 0; $i < (count($components) - 1); $i++) {
					$path .= $components[$i].SEPARATOR;					
				}				
				$template_file = $path.$tmp[0].".html.php"; // set template path
				$path .= $components[count($components) - 1]; // set code-behind				
				require_once($path); // require path file				
				
				// Object instance
				$tmp = explode(".", $view_file);
				$class_name = $tmp[0];							
				eval("\$_o = new ".$class_name."('".$template_file."');"); // load the template
			}
		}		
	}
}